/**
 * ajax调用方法
 * @param {Object} reqType 请求的类型（get，post）
 * @param {Object} reqURL 要请求的路径
 * @param {Object} reqPara 要传递的参数列表，如{ op: 1, key: 2 }
 * @return {Object} returndata 查询结果
 */
function callAJAX(reqType, reqURL, reqPara) {
	var returndata = '';
	$.ajax({
		type: reqType,
		url: reqURL,
		datatype: 'json',
		async: false,//不做异步刷新，解决执行顺讯问题
		data: reqPara,
		success: function(data) {
			returndata = data;
		},
		error: function() {
			returndata = '';
		}
	});
	return returndata;
}

/**
 * 阶段信息下拉框动态加载
 * @param {Object} controlName 要加载到的控件名称
 * @param {Object} form layui表单依赖参数form.render("select")，重新渲染
 */
function loadstage(controlName, form){
	var reqType = 'get';
	var reqURL = '../json/schoolType.json';
	var reqPara = {};
	var stageData = callAJAX(reqType, reqURL, reqPara)
	if(stageData != '' && stageData != undefined) {
		if(stageData.code == 0) {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择阶段</option>";
			for(var i = 0; i < stageData.count; i++) {
				str += '<option value=' + stageData.data[i].id + '>' + stageData.data[i].name + '</option>';
			}
			$('#' + controlName).append(str);
			form.render("select");
		} else {
			//layer.msg("未获取到阶段信息！");
			layer.msg('未获取到阶段信息！', function(){});
		}
	} else {
		//layer.msg("阶段信息获取失败！");
		layer.msg('未获取到阶段信息！', function(){});
	}
}

/**
 * 根据阶段下拉框加载学校信息
 * @param {Object} schoolType 学校类型（阶段）
 * @param {Object} controlName 要加载到的控件名称
 * @param {Object} form layui表单依赖参数form.render("select")，重新渲染
 */
function loadSchool(schoolType, controlName, form){
	var reqType = 'get';
	var reqURL = '../sysschoolmanagement/getschoolbywhere';
	var reqPara = {schooltype:schoolType};
	var schoolData = callAJAX(reqType, reqURL, reqPara)
	if(schoolData != '' && schoolData != undefined) {
		if(schoolData.code == 0) {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择学校</option>";
			for(var i = 0; i < schoolData.count; i++) {
				str += '<option value=' + schoolData.data[i].schoolid + '>' + schoolData.data[i].schoolName + '</option>';
			}
			$('#' + controlName).append(str);
			form.render("select");
		} else {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择学校</option>";
			$('#' + controlName).append(str);
			form.render("select");
			layer.msg('未获取到学校信息！', function(){});
		}
	} else {
		$('#' + controlName).html("");
		var str = "<option value='0'>请选择学校</option>";
		$('#' + controlName).append(str);
		form.render("select");
		layer.msg('学校信息获取失败！', function(){});
	}
}

/**
 * 年级下拉框动态加载请求方法
 * @param {Object} schoolType 学校类型代码
 * @param {Object} controlName 要加载到的控件名称
 * @param {Object} form layui表单依赖参数form.render("select")，重新渲染
 */
function loadgrade(schoolType, controlName, form) {
	if(schoolType == 10001) { //如果是10001代表是仅有小学
		var reqType = 'get';
		var reqURL = '../json/primary.json';
		var reqPara = {};
		var gradeData = callAJAX(reqType, reqURL, reqPara)
		if(gradeData != '' && gradeData != undefined) {
			if(gradeData.code == 0) {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				for(var i = 0; i < gradeData.count; i++) {
					str += '<option value=' + gradeData.data[i].gradename + '>' + gradeData.data[i].gradename + '</option>';
				}
				$('#' + controlName).append(str);
				form.render("select");
			} else {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				$('#' + controlName).append(str);
				form.render("select");
				layer.msg('未获取到年级信息！', function(){});
			}
		} else {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择年级</option>";
			$('#' + controlName).append(str);
			form.render("select");
			layer.msg('年级信息获取失败！', function(){});
		}
	} else if(schoolType == 10002) { //如果是10002代表是仅有初中
		var reqType = 'get';
		var reqURL = '../json/junior.json';
		var reqPara = {};
		var gradeData = callAJAX(reqType, reqURL, reqPara)
		if(gradeData != '' && gradeData != undefined) {
			if(gradeData.code == 0) {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				for(var i = 0; i < gradeData.count; i++) {
					str += '<option value=' + gradeData.data[i].gradename + '>' + gradeData.data[i].gradename + '</option>';
				}
				$('#' + controlName).append(str);
				form.render("select");
			} else {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				$('#' + controlName).append(str);
				form.render("select");
				layer.msg('未获取到年级信息！', function(){});
			}
		} else {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择年级</option>";
			$('#' + controlName).append(str);
			form.render("select");
			layer.msg('年级信息获取失败！', function(){});
		}
	} else if(schoolType == 10003) { //如果是10003代表是仅有高中
		var reqType = 'get';
		var reqURL = '../json/high.json';
		var reqPara = {};
		var gradeData = callAJAX(reqType, reqURL, reqPara)
		if(gradeData != '' && gradeData != undefined) {
			if(gradeData.code == 0) {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				for(var i = 0; i < gradeData.count; i++) {
					str += '<option value=' + gradeData.data[i].gradename + '>' + gradeData.data[i].gradename + '</option>';
				}
				$('#' + controlName).append(str);
				form.render("select");
			} else {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				$('#' + controlName).append(str);
				form.render("select");
				layer.msg('未获取到年级信息！', function(){});
			}
		} else {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择年级</option>";
			$('#' + controlName).append(str);
			form.render("select");
			layer.msg('年级信息获取失败！', function(){});
		}
	} else if(schoolType == 10004) { //如果是10004代表是小学和初中
		var reqType = 'get';
		var reqURL = '../json/primaryJunior.json';
		var reqPara = {};
		var gradeData = callAJAX(reqType, reqURL, reqPara)
		if(gradeData != '' && gradeData != undefined) {
			if(gradeData.code == 0) {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				for(var i = 0; i < gradeData.count; i++) {
					str += '<option value=' + gradeData.data[i].gradename + '>' + gradeData.data[i].gradename + '</option>';
				}
				$('#' + controlName).append(str);
				form.render("select");
			} else {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				$('#' + controlName).append(str);
				form.render("select");
				layer.msg('未获取到年级信息！', function(){});
			}
		} else {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择年级</option>";
			$('#' + controlName).append(str);
			form.render("select");
			layer.msg('年级信息获取失败！', function(){});
		}
	} else if(schoolType == 10005) { //如果是10005代表是初中和高中
		var reqType = 'get';
		var reqURL = '../json/juniorHigh.json';
		var reqPara = {};
		var gradeData = callAJAX(reqType, reqURL, reqPara)
		if(gradeData != '' && gradeData != undefined) {
			if(gradeData.code == 0) {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				for(var i = 0; i < gradeData.count; i++) {
					str += '<option value=' + gradeData.data[i].gradename + '>' + gradeData.data[i].gradename + '</option>';
				}
				$('#' + controlName).append(str);
				form.render("select");
			} else {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				$('#' + controlName).append(str);
				form.render("select");
				layer.msg('未获取到年级信息！', function(){});
			}
		} else {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择年级</option>";
			$('#' + controlName).append(str);
			form.render("select");
			layer.msg('年级信息获取失败！', function(){});
		}
	} else {
		var reqType = 'get';
		var reqURL = '../json/primaryJuniorHigh.json';
		var reqPara = {};
		var gradeData = callAJAX(reqType, reqURL, reqPara)
		if(gradeData != '' && gradeData != undefined) {
			if(gradeData.code == 0) {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				for(var i = 0; i < gradeData.count; i++) {
					str += '<option value=' + gradeData.data[i].gradename + '>' + gradeData.data[i].gradename + '</option>';
				}
				$('#' + controlName).append(str);
				form.render("select");
			} else {
				$('#' + controlName).html("");
				var str = "<option value='0'>请选择年级</option>";
				$('#' + controlName).append(str);
				form.render("select");
				layer.msg('未获取到年级信息！', function(){});
			}
		} else {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择年级</option>";
			$('#' + controlName).append(str);
			form.render("select");
			layer.msg('年级信息获取失败！', function(){});
		}
	}
}

/**
 * 根据年级和学校下拉框加载班级信息
 * @param {Object} schoolid 学校id
 * @param {Object} grade 年级信息
 * @param {Object} controlName 要加载到的控件名称
 * @param {Object} form layui表单依赖参数form.render("select")，重新渲染
 */
function loadClass(schoolID, gradeName, controlName, form){
	var reqType = 'get';
	var reqURL = '../sysclassmanagement/getclassbygrade';
	var reqPara = { schoolid:schoolID, grade:gradeName };
	var classData = callAJAX(reqType, reqURL, reqPara)
	if(classData != '' && classData != undefined) {
		if(classData.code == 0) {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择班级</option>";
			for(var i = 0; i < classData.count; i++) {
				str += '<option value=' + classData.data[i].classid + '>' + classData.data[i].className + '</option>';
			}
			$('#' + controlName).append(str);
			form.render("select");
		} else {
			$('#' + controlName).html("");
			var str = "<option value='0'>请选择班级</option>";
			$('#' + controlName).append(str);
			form.render("select");
			layer.msg('未获取到班级信息！', function(){});
		}
	} else {
		$('#' + controlName).html("");
		var str = "<option value='0'>请选择班级</option>";
		$('#' + controlName).append(str);
		form.render("select");
		layer.msg('班级信息获取失败！', function(){});
	}
}